package ac.manchester.cs.afzal.term_classifier.information_retrieval;

import ac.manchester.cs.afzal.term_classifier.miscellaneous.File_Manager_Impl;
import ac.manchester.cs.afzal.term_classifier.nlp.String_Manipulator;
import ac.manchester.cs.afzal.term_classifier.nlp.String_Manipulator_Impl;

import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.StringTokenizer;

/**
 * This class is obsoleted: Cannot be used with current Directory Structure of "Resources".
 * Title: Term Classifier
 *
 * A toolkit to find the relevance of candidate terms from a text corpus
 * to any particular topic/sub-domain using a term classification driven approach. The
 * system utilizes the lexical and contextual profiles of the candidate and domain-representing
 * "Resource Terms" (Seed and Ontological). The lexical and contextual profiles of candidate terms
 * are compared with those of Resource Terms to measure their relevance to the topic of interest.
 *
 * Download_Documents class contains the methods to download the pmcids whose index is given in
 * input file and downloads them into a directory
 * <p>Company: The University of Manchester</p>
 *
 * @author Hammad Afzal
 * @version 1.0
 * */

public class Download_Documents
{

    private String BASE_URL = "BASE_URL";
    private String TRAILING_URL = "TRAILING_URL";
    private String INPUT_DIR = "INPUT_DIR";
    private String OUTPUT_DIR = "OUTPUT_DIR";

    private List<String> pmc_ids = new ArrayList<String>();
    
    File_Manager_Impl fileManager = new File_Manager_Impl();

    public Download_Documents()
    {
        try
        {
            Properties props = new Properties();
            props.load(new FileInputStream("Term_Classification.properties"));
            BASE_URL = props.getProperty(BASE_URL);
            TRAILING_URL = props.getProperty(TRAILING_URL);
            INPUT_DIR = props.getProperty(INPUT_DIR);
            OUTPUT_DIR = props.getProperty(OUTPUT_DIR);
        }
        catch (Exception exception)
        {
            exception.printStackTrace();
        }
    }

    /**
     * Reads the list of pmcids from a text file and loads them into pmcids
     * @param input_file
     */
    public void read_pmcids_from_search_result_file(String input_file)
    {
        StringBuffer strBufPMCIDS = fileManager.fileReader(input_file);
        StringTokenizer st1 = new StringTokenizer(strBufPMCIDS.toString(), "\r\n");
        while(st1.hasMoreElements())
        {
            pmc_ids.add(st1.nextToken());
        }
    }

    /**
     * Reads the list of pmcids from Global variable pmc_ids and download the files
     * and write them into output_path directory/pmcid.html
     */
    private void download_pmcids()
    {
        for(String pmcid : pmc_ids)
        {
            String html_file = BASE_URL + pmcid + TRAILING_URL;
            fileManager.fileDownloader(html_file, OUTPUT_DIR + "\\" + pmcid + ".html");
        }
    }

    /**
     * Reads the list of pmcids from given input file and download the files
     * and write them into output_path directory/pmcid.html
     * @param input_file
     */
    public void download_pmcids(String input_file)
    {
        StringBuffer strBufPMCIDS = fileManager.fileReader(INPUT_DIR + input_file);
        StringTokenizer st1 = new StringTokenizer(strBufPMCIDS.toString(), "\r\n");
        while(st1.hasMoreElements())
        {
            pmc_ids.add(st1.nextToken());
        }
        download_pmcids();
    }

    /**
     * Reads the list of pmcids from given input file and download the files
     * and write them into output_path directory/pmcid.html
     * @param input_file
     */
    public void read_search_result()
    {
        StringBuffer contents = null;
        String_Manipulator str_man = new String_Manipulator_Impl();

        contents = fileManager.fileReader("Resources//Search_Results//PMCIDs.xml");


        String result_count = str_man.string_extractor(contents.toString(), "<Count>", "</Count>");

        /*----------------------- Start - Code for Reading PMIDS -----------------------------------*/
        int index = 0;
        String pmc_id = "";
        while((index = contents.indexOf("<Id>", index)) != -1)
        {
            index ++;
            String remaining = contents.substring(index);
            StringTokenizer st = new StringTokenizer(remaining, "</Id>");
            pmc_id = st.nextToken();
            pmc_ids.add(pmc_id);
        }
        /*----------------------- End - Code for Reading PMIDs -----------------------------------*/
    }

    /**
     * @return the pmc_ids
     */
    public List<String> get_pmc_ids()
    {
        return pmc_ids;
    }

}
